﻿Public Class FrmConsecutivo

    Private Sub FrmConsecutivo_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarCmbVig()
        cargarGvConsecutivo()
        btNuevo.Focus()
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As Object, e As System.EventArgs)
        MyBase.bnNuevo_Click(sender, e)
        cargarGvConsecutivo()
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As Object, e As System.EventArgs)
        If Not Validaciones.camposVacios(pBase) Then
            If guardar() Then
                MyBase.bnGuardar_Click(sender, e)
                cargarGvConsecutivo()
            End If
        End If
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As Object, e As System.EventArgs)
        If tbCodigo.TextLength < 1 Then
            msnBox.msnExclamacion("Digite el Codigo")

        Else
            If msnBox.msnQuestion("¿Esta Seguro de eliminar el Consecutivo?") = Windows.Forms.DialogResult.Yes Then
                If eliminar() Then
                    MyBase.bnEliminar_Click(sender, e)
                    cargarGvConsecutivo()
                End If
            End If

        End If
    End Sub

    Private Sub gvConsecutivo_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvConsecutivo.CellClick
        cargarConsecutivo()
    End Sub

    Private Sub tbNumero_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbNumero.KeyPress
        Validaciones.SoloNumeros(sender, e)
    End Sub

    Private Function guardar() As Boolean
        Dim result As GBDZafiro.Resultado
        Dim cons As GBDZafiro.Consecutivo = crearConsecutivo()
        If GBDZafiro.Consecutivo.exist(cons) Then
            result = GBDZafiro.Consecutivo.Actualizar(cons)
        Else
            result = GBDZafiro.Consecutivo.Nuevo(cons)
        End If

        If Not result.ExisteError Then
            msnBox.msnError(result.Mensaje)
        End If

        Return result.ExisteError
    End Function

    Private Function eliminar() As Boolean
        Dim result As GBDZafiro.Resultado = GBDZafiro.Consecutivo.Eliminar(crearConsecutivo())
        If Not result.ExisteError Then
            msnBox.msnError(result.Mensaje)
        End If
        Return result.ExisteError
    End Function

    Private Function crearConsecutivo() As GBDZafiro.Consecutivo
        Dim cons As New GBDZafiro.Consecutivo
        cons.CODIGO = tbCodigo.Text.ToUpper
        cons.DESCRIPCION = tbDescripcion.Text.ToUpper
        cons.FECHA = dtpFecha.Value
        cons.NUMERO = CDec(tbNumero.Text)
        cons.VIGENCIA = cmbVigencia.SelectedValue
        Return cons
    End Function

    Private Sub cargarCmbVig()
        cmbVigencia.DataSource = GBDZafiro.Vigencia.ConsultarAll()
        cmbVigencia.ValueMember = "VIGENCIA"
        cmbVigencia.DisplayMember = "VIGENCIA"
    End Sub

    Private Sub cargarGvConsecutivo()
        gvConsecutivo.DataSource = Nothing
        gvConsecutivo.DataSource = GBDZafiro.Consecutivo.ConsultarAll()
    End Sub

    Private Sub cargarConsecutivo()
        Dim rCons As DataGridViewRow = gvConsecutivo.CurrentRow
        tbCodigo.Text = rCons.Cells("CODIGO").Value
        tbDescripcion.Text = rCons.Cells("DESCRIPCION").Value
        tbNumero.Text = rCons.Cells("NUMERO").Value
        dtpFecha.Value = rCons.Cells("FECHA").Value
        cmbVigencia.SelectedValue = rCons.Cells("VIGENCIA").Value
    End Sub

End Class
